package com.rxt.thermopro.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.BuildConfig
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.viewmodel.EventViewModel
import kotlinx.android.synthetic.main.fragment_setting.*

class SettingFragment : BaseFragment<BaseViewModel>(), View.OnClickListener {

    override fun layoutId(): Int {
        return R.layout.fragment_setting
    }

    companion object {
        fun newInstance(): SettingFragment {
            return SettingFragment()
        }
    }

    private lateinit var mEventViewModel: EventViewModel

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        mEventViewModel = ViewModelProvider(requireActivity()).get(EventViewModel::class.java)
        backButton.setOnClickListener {
            println("123-> exit SettingFragment")
            requireActivity().onBackPressed()
        }
        val isUnitC = LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C
        textC.isSelected = isUnitC
        switchUnit.isSelected = isUnitC
        textF.isSelected = !isUnitC
        viewVersion.text = "V" + BuildConfig.VERSION_NAME
        setListener()
    }

    private fun setListener() {
        switchButton.setOnClickListener {
            switchUnit.isSelected = !switchUnit.isSelected
            textC.isSelected = switchUnit.isSelected
            textF.isSelected = !switchUnit.isSelected
            if (textC.isSelected) {
                LocalDataDao.INSTANCE.setTempUnit(LocalDataDao.TEMP_UNIT_C)
            } else {
                LocalDataDao.INSTANCE.setTempUnit(LocalDataDao.TEMP_UNIT_F)
            }
            mEventViewModel.refreshUnit()
        }

//        linear2.setOnClickListener(this)
        linear3.setOnClickListener(this)
        linear4.setOnClickListener(this)
        linear5.setOnClickListener(this)
        linear6.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.linear2 -> {
                addFragment(FAQSFragment.newInstance())
            }
            R.id.linear3 -> {
                addFragment(ContactFragment.newInstance())
            }
            R.id.linear4 -> {
                addFragment(RegistrationFragment.newInstance())
            }
            R.id.linear5 -> {

            }
            R.id.linear6 -> {
                addFragment(AboutFragment.newInstance())
            }
        }
    }

    private fun addFragment(fragment: Fragment) {
        parentFragmentManager.beginTransaction()
            .setCustomAnimations(
                R.anim.translate_into, R.anim.translate_out,
                R.anim.translate_into_pop, R.anim.translate_out_exit
            )
            .add(
                R.id.mainContent,
                fragment
            )
            .addToBackStack("main")
            .commit()
    }

}